<script type="text/javascript">
    /**
     * Created by JetBrains RubyMine.
     * User: liupengtao.pt
     * Date: 11-7-25
     * Time: 下午4:38
     * To change this template use File | Settings | File Templates.
     */
    //The Welcome bar in the top
    Ext.onReady(function() {
        var welcomePanel = {
            region:'north',
            contentEl:'north',
            frame:true
        };

        var adminMenus = [];

        function addAdminMenu(id, text, leaf) {
            adminMenus[adminMenus.length] = {
                id:id,
                text:text,
                leaf:leaf
            };
        }

        addAdminMenu(0, '用户信息管理', true);
        addAdminMenu(1, '角色管理', true);
        addAdminMenu(2, '指令管理', true);
        addAdminMenu(3, '指令组管理', true);
        addAdminMenu(4, '应用管理', true);
        addAdminMenu(5, '机房管理', true);
        addAdminMenu(6, '机器管理', true);

        //管理菜单树
        var adminMenuTreeStore = Ext.create('Ext.data.TreeStore', {
            root:{
                expanded:true,
                children:adminMenus
            }
        });
        var adminMenuTreePanel = Ext.create('Ext.tree.Panel', {
            title:'管理菜单',
            region:'west',
            bodyCls:'admin_nav_menu',
            rootVisible:false,
            split:true,
            collapsible:true,
            width:220,
            store:adminMenuTreeStore
        });

        //用户信息管理面板
        var userInfoPanel = {
            id:'0',
            title:'用户信息管理',
            frame:true,
            bodyPadding:5,
            layout:'border',
            split:true,
            fieldDefaults: {
                labelAlign: 'left',
                msgTarget: 'side'
            },
            items: [
                {
                    region:'center',
                    xtype: 'gridpanel',
                    title:'当前系统所有用户',
                    store:userGridStore,
                    split:true,
                    columnLines:true,
                    viewConfig: {
                        stripeRows: true
                    },
                    selType: 'rowmodel',
                    plugins: [
                        userPanelRowEditing
                    ],
                    columns:[
                        {
                            text:'用户名',
                            dataIndex:'email',
                            flex:1,
                            editor: {
                                xtype: 'textfield',
                                allowBlank: false
                            }
                        },
                        {
                            flex:1,
                            text:'本次登录时间',
                            dataIndex:'current_sign_in_at',
                            renderer:Ext.util.Format.dateRenderer('Y-m-d H:i:s')
                        },
                        {
                            flex:1,
                            text:'上次登录时间',
                            dataIndex:'last_sign_in_at',
                            renderer:Ext.util.Format.dateRenderer('Y-m-d H:i:s')
                        },
                        {
                            flex:1,
                            text:'本次登录IP',
                            dataIndex:'current_sign_in_ip'
                        },
                        {
                            flex:1,
                            text:'上次登录IP',
                            dataIndex:'last_sign_in_ip'
                        },
                        {
                            flex:1,
                            xtype: 'actioncolumn',
                            items: [
                                {
                                    icon   :"<%= root_path %>" + 'stylesheets/images/delete.gif',
                                    tooltip: '删除当前用户',
                                    handler: function(grid, rowIndex, colIndex) {
                                        var r = userGridStore.getAt(rowIndex);
                                        Ext.Ajax.request({
                                            url:"<%=admin_users_path %>/" + r.get('id'),
                                            method:'DELETE',
                                            params:{
                                                authenticity_token:$('meta[name="csrf-token"]').attr('content')
                                            },
                                            callback:function (options, success, response) {

                                            }
                                        });
                                        userGridStore.remove(r);
                                        userGridStore.load();
                                    }
                                }
                            ]
                        }
                    ],
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: userGridStore,
                        displayInfo: true
                    }),
                    tbar: [
                        {
                            text: '增加用户',
                            iconCls:'add',
                            handler : function() {
                                var addUserWin = Ext.create('Ext.Window', {
                                    title:'增加用户',
                                    layout:'border',
                                    width:300,
                                    height:170,
                                    items:[
                                        Ext.create('Ext.form.Panel', {
                                            region:'center',
                                            frame:'true',
                                            url:"<%=admin_users_path %>/",
                                            defaultType:'textfield',
                                            defaults: {
                                                labelWidth:90,
                                                anchor:'95%'
                                            },
                                            items:[
                                                {
                                                    xtype:'hidden',
                                                    name:'authenticity_token',
                                                    value:Ext.select('meta[name="csrf-token"]').elements[0].getAttribute('content')
                                                },
                                                {
                                                    fieldLabel:'用户名',
                                                    name:'user[email]',
                                                    allowBlank:false,
                                                    blankText:'用户名不能为空'
                                                },
                                                {
                                                    fieldLabel:'密码',
                                                    name:'user[password]',
                                                    id:'user_pwd',
                                                    inputType:'password',
                                                    allowBlank:false,
                                                    blankText:'密码不能为空'
                                                },
                                                {
                                                    fieldLabel:'重复密码',
                                                    name:'user[password_confirmation]',
                                                    inputType:'password',
                                                    id:'repeat_pwd',
                                                    allowBlank:false,
                                                    blankText:'密码不能为空'
                                                }
                                            ],
                                            buttons:[
                                                {
                                                    text:'保存',
                                                    handler:function() {
                                                        var form = this.up('form').getForm();
                                                        if (form.isValid()) {
                                                            var pwd = Ext.getCmp('user_pwd').value;
                                                            var repeat_pwd = Ext.getCmp('repeat_pwd').value;
                                                            if (pwd !== repeat_pwd) {
                                                                Ext.Msg.alert('错误', '两次输入密码不一致，请重新输入');
                                                                return;
                                                            }
                                                            form.submit({
                                                                success: function(form, action) {
                                                                    userGridStore.load();
                                                                    addUserWin.close();
                                                                },
                                                                failure: function(form, action) {
                                                                    userGridStore.load();
                                                                    addUserWin.close();
                                                                }
                                                            });
                                                        }
                                                    }
                                                },
                                                {
                                                    text:'取消',
                                                    handler:function() {
                                                        this.up('window').close();
                                                    }
                                                }
                                            ]
                                        })
                                    ]
                                });
                                addUserWin.show();
                            }
                        }
                    ],
                    listeners: {
                        edit:function(editor, e) {
                            editor.record.commit();
                            var record = editor.record;
                            Ext.Ajax.request({
                                url:"<%=admin_users_path %>/" + record.get('id'),
                                method:'PUT',
                                params:{
                                    authenticity_token:Ext.select('meta[name="csrf-token"]').elements[0].getAttribute('content'),
                                    'user[name]':record.get('name')
                                },
                                callback:function(options, success, response) {

                                }
                            });
                        }
                    }
                }
            ]
        };
        //角色管理面板
        var rolePanel = {
            id:'1',
            frame:true,
            title:'角色管理',
            layout:'border',
            split:true,
            bodyPadding:5,
            fieldDefaults: {
                labelAlign: 'left',
                msgTarget: 'side'
            },
            items: [
                {
                    xtype:'gridpanel',
                    title:'当前系统所有角色',
                    store:roleGridStore,
                    region:'center',
                    columnLines:true,
                    viewConfig: {
                        stripeRows: true
                    },
                    selType: 'rowmodel',
                    plugins: [
                        rolePanelRowEditing
                    ],
                    columns:[
                        {
                            text:'角色名',
                            dataIndex:'name',
                            flex:1,
                            editor: {
                                xtype: 'textfield',
                                allowBlank: false
                            }
                        },
                        {
                            flex:1,
                            xtype: 'actioncolumn',
                            items: [
                                {
                                    icon   :"<%= root_path %>" + 'stylesheets/images/delete.gif',
                                    tooltip: '删除当前角色',
                                    handler: function(grid, rowIndex, colIndex) {
                                        var r = roleGridStore.getAt(rowIndex);
                                        Ext.Ajax.request({
                                            url:"<%=admin_roles_path %>/" + r.get('id'),
                                            method:'DELETE',
                                            params:{
                                                authenticity_token:$('meta[name="csrf-token"]').attr('content')
                                            },
                                            callback:function (options, success, response) {

                                            }
                                        });
                                        roleGridStore.remove(r);
                                        roleGridStore.load();
                                    }
                                }
                            ]
                        }
                    ],
                    tbar: [
                        {
                            text: '增加角色',
                            iconCls:'add',
                            handler : function() {
                                var addRoleWin = Ext.create('Ext.Window', {
                                    title:'增加角色',
                                    layout:'border',
                                    width:300,
                                    height:150,
                                    items:[
                                        Ext.create('Ext.form.Panel', {
                                            region:'center',
                                            frame:'true',
                                            url:"<%=admin_roles_path %>/",
                                            defaultType:'textfield',
                                            defaults: {
                                                labelWidth:90,
                                                anchor:'95%'
                                            },
                                            items:[
                                                {
                                                    xtype:'hidden',
                                                    name:'authenticity_token',
                                                    value:$('meta[name="csrf-token"]').attr('content')
                                                },
                                                {
                                                    fieldLabel:'角色名',
                                                    name:'role[name]',
                                                    allowBlank:false,
                                                    blankText:'角色名不能为空'
                                                }
                                            ],
                                            buttons:[
                                                {
                                                    text:'保存',
                                                    handler:function() {
                                                        var form = this.up('form').getForm();
                                                        if (form.isValid()) {
                                                            form.submit({
                                                                success: function(form, action) {
                                                                    roleGridStore.load();
                                                                    addRoleWin.close();
                                                                },
                                                                failure: function(form, action) {
                                                                    roleGridStore.load();
                                                                    addRoleWin.close();
                                                                }
                                                            });
                                                        }
                                                    }
                                                },
                                                {
                                                    text:'取消',
                                                    handler:function() {
                                                        this.up('window').close();
                                                    }
                                                }
                                            ]
                                        })
                                    ]
                                });
                                addRoleWin.show();
                            }
                        }
//                    {
//                        iconCls:'delete',
//                        disabled:true
//                    },
//                    {
//                        boxLabel:'删除机房时同时删除其下的所有机器',
//                        xtype:'checkbox',
//                        id:'cascadeMachine'
//                    }
                    ],
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: roleGridStore,
                        displayInfo: true
                    }),
                    listeners: {
                        edit:function(editor, e) {
                            editor.record.commit();
                            var record = editor.record;
                            Ext.Ajax.request({
                                url:"<%=admin_roles_path %>/" + record.get('id'),
                                method:'PUT',
                                params:{
                                    authenticity_token:$('meta[name="csrf-token"]').attr('content'),
                                    'role[name]':record.get('name')
                                },
                                callback:function(options, success, response) {

                                }
                            });
                        }
                    }
                }
            ]
        };

        //指令管理面板
        var cmdDefPanel = Ext.create('Ext.form.Panel', {
            frame:true,
            id:'2',
            title:'指令管理',
            bodyPadding:5,
            layout:'border',
            split:true,
            fieldDefaults: {
                labelAlign: 'left',
                msgTarget: 'side'
            },
            items: [
                {
                    region:'center',
                    xtype: 'gridpanel',
                    title:'当前系统所有指令',
                    store:cmdDefGridStore,
                    split:true,
                    columnLines:true,
                    viewConfig: {
                        stripeRows: true
                    },
                    selType: 'rowmodel',
                    plugins: [
                        cmdDefPanelRowEditing
                    ],
                    columns:[
                        {
                            text:'指令名',
                            dataIndex:'name',
                            flex:6,
                            editor: {
                                xtype: 'textfield',
                                allowBlank: false
                            }
                        },
                        {
                            text:'别名',
                            dataIndex:'alias',
                            flex:6,
                            editor: {
                                xtype:'textfield'
                            }
                        },
                        {
                            text:'参数1',
                            dataIndex:'arg1',
                            flex:2,
                            editor: {
                                xtype:'textfield'
                            }
                        },
                        {
                            text:'参数2',
                            dataIndex:'arg2',
                            flex:2,
                            editor: {
                                xtype:'textfield'
                            }
                        },
                        {
                            text:'参数3',
                            dataIndex:'arg3',
                            flex:2,
                            editor: {
                                xtype:'textfield'
                            }
                        },
                        {
                            text:'参数4',
                            dataIndex:'arg4',
                            flex:2,
                            editor: {
                                xtype:'textfield'
                            }
                        },
                        {
                            text:'参数5',
                            dataIndex:'arg5',
                            flex:2,
                            editor: {
                                xtype:'textfield'
                            }
                        },
                        {
                            text:'指令组',
                            dataIndex:'directive_group_id',
                            flex:4,
                            editor: {
                                xtype:'combo',
                                name:'directive_group_id',
                                valueField:'id',
                                displayField:'name',
                                store:editDirectiveTemplateDirectiveGroupComboStore,
                                editable:false
                            },
                            renderer:cmdGroupRender
                        },
                        {
                            flex:1,
                            xtype: 'actioncolumn',
                            items: [
                                {
                                    icon   :"<%= root_path %>" + 'stylesheets/images/delete.gif',
                                    tooltip: '删除当前指令',
                                    handler: function(grid, rowIndex, colIndex) {
                                        var r = cmdDefGridStore.getAt(rowIndex);
                                        Ext.Ajax.request({
                                            url:"<%=admin_directive_templates_path %>/" + r.get('id'),
                                            method:'DELETE',
                                            params:{
                                                authenticity_token:$('meta[name="csrf-token"]').attr('content')
                                            },
                                            callback:function (options, success, response) {

                                            }
                                        });
                                        cmdDefGridStore.remove(r);
                                        cmdDefGridStore.reload();
                                    }
                                }
                            ]
                        }
                    ],
                    tbar: [
                        {
                            text: '增加指令',
                            iconCls:'add',
                            handler : function() {
                                var addDirectiveTemplateWin = Ext.create('Ext.Window', {
                                    title:'增加指令',
                                    layout:'border',
                                    width:500,
                                    items:[
                                        Ext.create('Ext.form.Panel', {
                                            region:'center',
                                            frame:'true',
                                            url:"<%=admin_directive_templates_path %>/",
                                            defaultType:'textfield',
                                            defaults: {
                                                labelWidth:90,
                                                anchor:'95%'
                                            },
                                            items:[
                                                {
                                                    xtype:'hidden',
                                                    name:'authenticity_token',
                                                    value:$('meta[name="csrf-token"]').attr('content')
                                                },
                                                {
                                                    fieldLabel:'指令名',
                                                    name:'directive_template[name]',
                                                    allowBlank:false,
                                                    blankText:'指令名不能为空'
                                                },
                                                {
                                                    fieldLabel:'别名',
                                                    name:'directive_template[alias]'
                                                },
                                                {
                                                    fieldLabel:'参数1',
                                                    name:'directive_template[arg1]'
                                                },
                                                {
                                                    fieldLabel:'参数2',
                                                    name:'directive_template[arg2]'
                                                },
                                                {
                                                    fieldLabel:'参数3',
                                                    name:'directive_template[arg3]'
                                                },
                                                {
                                                    fieldLabel:'参数4',
                                                    name:'directive_template[arg4]'
                                                },
                                                {
                                                    fieldLabel:'参数5',
                                                    name:'directive_template[arg5]'
                                                },
                                                {
                                                    fieldLabel:'指令组',
                                                    xtype:'combo',
                                                    name:'directive_template[directive_group_id]',
                                                    valueField:'id',
                                                    displayField:'name',
                                                    store:addDirectiveTemplateDirectiveGroupComboStore,
                                                    editable:false
                                                }
                                            ],
                                            buttons:[
                                                {
                                                    text:'保存',
                                                    handler:function() {
                                                        var form = this.up('form').getForm();
                                                        if (form.isValid()) {
                                                            form.submit({
                                                                success: function(form, action) {
                                                                    cmdDefGridStore.load();
                                                                    addDirectiveTemplateWin.close();
                                                                },
                                                                failure: function(form, action) {
                                                                    cmdDefGridStore.load();
                                                                    addDirectiveTemplateDirectiveGroupComboStore.load();
                                                                    addDirectiveTemplateWin.close();
                                                                }
                                                            });
                                                        }
                                                    }
                                                },
                                                {
                                                    text:'取消',
                                                    handler:function() {
                                                        this.up('window').close();
                                                    }
                                                }
                                            ]
                                        })
                                    ]
                                });
                                addDirectiveTemplateWin.show();
                            }
                        }
                    ],
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: cmdDefGridStore,
                        displayInfo: true
                    }),
                    listeners: {
                        edit:function(editor, e) {
                            editor.record.commit();
                            var record = editor.record;
                            Ext.Ajax.request({
                                url:"<%=admin_directive_templates_path %>/" + record.get('id'),
                                method:'PUT',
                                params:{
                                    authenticity_token:$('meta[name="csrf-token"]').attr('content'),
                                    'directive_template[name]':record.get('name'),
                                    'directive_template[alias]':record.get('alias'),
                                    'directive_template[arg1]':record.get('arg1'),
                                    'directive_template[arg2]':record.get('arg2'),
                                    'directive_template[arg3]':record.get('arg3'),
                                    'directive_template[arg4]':record.get('arg4'),
                                    'directive_template[arg5]':record.get('arg5'),
                                    'directive_template[directive_group_id]':record.get('directive_group_id')
                                },
                                callback:function(options, success, response) {

                                }
                            });
                        }
                    }
                }
            ]
        });
        //指令组管理面板
        var cmdGroupDefPanel = {
            frame:true,
            id:'3',
            title:'指令组管理',
            bodyPadding:5,
            layout:'border',
            split:true,
            fieldDefaults: {
                labelAlign: 'left',
                msgTarget: 'side'
            },
            items: [
                {
                    region:'center',
                    xtype: 'gridpanel',
                    title:'当前系统所有指令组',
                    store:cmdGroupGridStore,
                    split:true,
                    columnLines:true,
                    viewConfig: {
                        stripeRows: true
                    },
                    selType: 'rowmodel',
                    plugins: [
                        cmdGroupPanelRowEditing
                    ],
                    columns:[
                        {
                            text:'指令组名',
                            dataIndex:'name',
                            flex:1,
                            editor: {
                                xtype: 'textfield',
                                allowBlank: false
                            }
                        },
                        {
                            flex:1,
                            xtype: 'actioncolumn',
                            items: [
                                {
                                    icon   : "<%= root_path %>" + 'stylesheets/images/delete.gif',
                                    tooltip: '删除当前指令组',
                                    handler: function(grid, rowIndex, colIndex) {
                                        var r = cmdGroupGridStore.getAt(rowIndex);
                                        var cascade = Ext.getCmp('cascade').checked;
                                        Ext.Ajax.request({
                                            url:"<%=admin_directive_groups_path %>/" + r.get('id'),
                                            method:'DELETE',
                                            params:{
                                                authenticity_token:$('meta[name="csrf-token"]').attr('content'),
                                                cascade:cascade
                                            },
                                            callback:function (options, success, response) {

                                            }
                                        });
                                        cmdGroupGridStore.remove(r);
                                        cmdGroupGridStore.load();
                                    }
                                }
                            ]
                        }
                    ],
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: cmdGroupGridStore,
                        displayInfo: true
                    }),
                    tbar: [
                        {
                            text: '增加指令组',
                            iconCls:'add',
                            handler : function() {
                                var addDirectiveGroupWin = Ext.create('Ext.Window', {
                                    title:'增加指令组',
                                    layout:'border',
                                    width:300,
                                    height:150,
                                    items:[
                                        Ext.create('Ext.form.Panel', {
                                            region:'center',
                                            frame:'true',
                                            url:"<%=admin_directive_groups_path %>/",
                                            defaultType:'textfield',
                                            defaults: {
                                                labelWidth:90,
                                                anchor:'95%'
                                            },
                                            items:[
                                                {
                                                    xtype:'hidden',
                                                    name:'authenticity_token',
                                                    value:$('meta[name="csrf-token"]').attr('content')
                                                },
                                                {
                                                    fieldLabel:'指令组名',
                                                    name:'directive_group[name]',
                                                    allowBlank:false,
                                                    blankText:'指令组名不能为空'
                                                }
                                            ],
                                            buttons:[
                                                {
                                                    text:'保存',
                                                    handler:function() {
                                                        var form = this.up('form').getForm();
                                                        if (form.isValid()) {
                                                            form.submit({
                                                                success: function(form, action) {
                                                                    cmdGroupGridStore.load();
                                                                    editDirectiveTemplateDirectiveGroupComboStore.load();
                                                                    addDirectiveGroupWin.close();
                                                                },
                                                                failure: function(form, action) {
                                                                    cmdGroupGridStore.load();
                                                                    editDirectiveTemplateDirectiveGroupComboStore.load();
                                                                    addDirectiveGroupWin.close();
                                                                }
                                                            });
                                                        }
                                                    }
                                                },
                                                {
                                                    text:'取消',
                                                    handler:function() {
                                                        this.up('window').close();
                                                    }
                                                }
                                            ]
                                        })
                                    ]
                                });
                                addDirectiveGroupWin.show();
                            }
                        },
                        {
                            iconCls:'delete',
                            disabled:true
                        },
                        {
                            boxLabel:'删除指令组时同时删除其下的所有指令',
                            xtype:'checkbox',
                            id:'cascade'
                        }
                    ],
                    listeners: {
                        edit:function(editor, e) {
                            editor.record.commit();
                            var record = editor.record;
                            Ext.Ajax.request({
                                url:"<%=admin_directive_groups_path %>/" + record.get('id'),
                                method:'PUT',
                                params:{
                                    authenticity_token:$('meta[name="csrf-token"]').attr('content'),
                                    'directive_group[name]':record.get('name')
                                },
                                callback:function(options, success, response) {

                                }
                            });
                        }
                    }
                }
            ]
        };

        //应用管理面板
        var appPanel = {
            id:'4',
            title:'应用管理',
            frame:true,
            bodyPadding:5,
            layout:'border',
            split:true,
            fieldDefaults: {
                labelAlign: 'left',
                msgTarget: 'side'
            },
            items:[
                {
                    region:'center',
                    xtype:'gridpanel',
                    title:'当前系统所有应用',
                    store:appGridStore,
                    split:true,
                    columnLines:true,
                    viewConfig: {
                        stripeRows: true
                    },
                    columns:[
                        {
                            text:'应用名',
                            dataIndex:'name',
                            flex:1
                        },
                        {
                            text:'创建日期',
                            dataIndex:'created_at',
                            flex:1,
                            renderer:Ext.util.Format.dateRenderer('Y-m-d H:i:s')
                        },
                        {
                            flex:1,
                            xtype: 'actioncolumn',
                            items: [
                                {
                                    icon   : "<%= root_path %>" + 'stylesheets/images/delete.gif',
                                    tooltip: '删除当前应用',
                                    handler: function(grid, rowIndex, colIndex) {
                                        var r = appGridStore.getAt(rowIndex);
                                        Ext.Ajax.request({
                                            url:"<%= admin_apps_path %>/" + r.get('id'),
                                            method:'DELETE',
                                            params:{
                                                authenticity_token:$('meta[name="csrf-token"]').attr('content')
                                            },
                                            callback:function (options, success, response) {

                                            }
                                        });
                                        appGridStore.remove(r);
                                        appGridStore.load();
                                    }
                                }
                            ]
                        },
                        {
                            flex:1,
                            xtype: 'actioncolumn',
                            items: [
                                {
                                    icon   : "<%= root_path %>" + 'stylesheets/images/edit.png',
                                    tooltip: '编辑',
                                    handler: function(grid, rowIndex, colIndex) {
                                        var record = grid.getStore().getAt(rowIndex);
                                        var editAppStore = Ext.create('Ext.data.Store', {
                                            fields:['id','app_id','user_id','role_id','user','role'],
                                            storeId:'editAppStore' + record.get('id'),
                                            proxy:{
                                                url:"<%= admin_apps_path %>/" + record.get('id'),
                                                type:'ajax',
                                                reader:{
                                                    type:'json'
                                                }
                                            },
                                            autoLoad:true
                                        });
                                        var userStore = Ext.create('Ext.data.Store', {
                                            fields:['id','email'],
                                            proxy:{
                                                url:"<%= admin_users_path %>",
                                                type:'ajax',
                                                reader:{
                                                    type:'json',
                                                    totalProperty:'totalCount',
                                                    root:'users'
                                                }
                                            },
                                            autoLoad:true
                                        });
                                        var roleStore = Ext.create('Ext.data.Store', {
                                            fields:['id','name'],
                                            proxy:{
                                                url:"<%= admin_roles_path %>",
                                                type:'ajax',
                                                reader:{
                                                    type:'json',
                                                    totalProperty:'totalCount',
                                                    root:'roles'
                                                }
                                            },
                                            autoLoad:true
                                        });
                                        var editAppWin = Ext.create('Ext.Window', {
                                            title:'管理当前应用的用户角色',
                                            width:500,
                                            height:400,
                                            layout:'border',
                                            modal:true,
                                            items:[
                                                {
                                                    xtype:'panel',
                                                    region:'north',
                                                    layout:'column',
                                                    defaults:{
                                                        margin:'0 0 0 5'
                                                    },
                                                    frame:true,
                                                    items:[
                                                        {
                                                            xtype:'textfield',
                                                            fieldLabel:'应用名',
                                                            name:'app[name]',
                                                            value:record.get('name'),
                                                            labelWidth:60,
                                                            columnWidth:0.65
                                                        },
                                                        {
                                                            xtype:'button',
                                                            text:'保存',
                                                            columnWidth:0.1,
                                                            handler:function() {
                                                                Ext.Ajax.request({
                                                                    url:"<%= admin_apps_path %>/" + record.get('id'),
                                                                    method:'PUT',
                                                                    params:{
                                                                        authenticity_token:$('meta[name="csrf-token"]').attr('content'),
                                                                        'app[name]':this.previousSibling('textfield').value
//                                                                        'app[online]':this.previousSibling('checkboxfield').checked
                                                                    },
                                                                    callback:function() {
                                                                        appGridStore.load();
                                                                    }
                                                                });
                                                            }
                                                        }
                                                    ]
                                                },
                                                {
                                                    xtype:'gridpanel',
                                                    title:'当前应用关联的所有的用户与角色',
                                                    region:'center',
                                                    split:true,
                                                    columnLines:true,
                                                    viewConfig: {
                                                        stripeRows: true
                                                    },
                                                    selType: 'rowmodel',
                                                    plugins: [
                                                        appPanelRowEditing
                                                    ],
                                                    store: editAppStore,
                                                    columns:[
                                                        {
                                                            text:'用户',
                                                            dataIndex:'user_id',
                                                            flex:1,
                                                            editor: {
                                                                xtype:'combo',
                                                                valueField:'id',
                                                                displayField:'email',
                                                                store:userStore,
                                                                editable:false
                                                            },
                                                            renderer:function(value) {
                                                                var r = userStore.getById(value);
                                                                return r.get('email');
                                                            }
                                                        },
                                                        {
                                                            text:'角色',
                                                            dataIndex:'role_id',
                                                            flex:1,
                                                            editor: {
                                                                xtype:'combo',
                                                                valueField:'id',
                                                                displayField:'name',
                                                                store:roleStore,
                                                                editable:false
                                                            },
                                                            renderer:function(value) {
                                                                var r = roleStore.getById(value);
                                                                return r.get('name');
                                                            }
                                                        },
                                                        {
                                                            flex:1,
                                                            xtype: 'actioncolumn',
                                                            items: [
                                                                {
                                                                    icon   : "<%= root_path %>" + 'stylesheets/images/delete.gif',
                                                                    handler: function(grid, rowIndex, colIndex) {
                                                                        var stateholderStore = grid.getStore();
                                                                        var r = stateholderStore.getAt(rowIndex);
                                                                        Ext.Ajax.request({
                                                                            url:"<%= admin_apps_path %>/" + r.get('app_id'),
                                                                            method:'DELETE',
                                                                            params:{
                                                                                authenticity_token:$('meta[name="csrf-token"]').attr('content'),
                                                                                user_id:r.get('user_id'),
                                                                                role_id:r.get('role_id')
                                                                            },
                                                                            callback:function (options, success, response) {

                                                                            }
                                                                        });
                                                                        stateholderStore.remove(r);
                                                                        stateholderStore.load();
                                                                    }
                                                                }
                                                            ]
                                                        }
                                                    ],
                                                    tbar:[
                                                        {
                                                            text: '增加新的用户角色',
                                                            iconCls:'set_app_for_user',
                                                            handler:function() {
                                                                var editAppForUserWin = Ext.create('Ext.Window', {
                                                                    title:'为应用分配用户',
                                                                    layout:'border',
                                                                    width:300,
                                                                    height:180,
                                                                    modal:true,
                                                                    items:[
                                                                        Ext.create('Ext.form.Panel', {
                                                                            region:'center',
                                                                            frame:'true',
                                                                            url:"<%= admin_apps_path %>",
                                                                            defaultType:'combo',
                                                                            defaults: {
                                                                                labelWidth:90,
                                                                                anchor:'95%'
                                                                            },
                                                                            layout:'column',
                                                                            items:[
                                                                                {
                                                                                    xtype:'hidden',
                                                                                    name:'authenticity_token',
                                                                                    value:$('meta[name="csrf-token"]').attr('content')
                                                                                },
                                                                                {
                                                                                    xtype:'hidden',
                                                                                    name:'dispatch_to_user',
                                                                                    value:true
                                                                                },
                                                                                {
                                                                                    fieldLabel:'选择应用',
                                                                                    xtype:'hidden',
                                                                                    name:'app_id',
                                                                                    value:record.get('id')
                                                                                },
                                                                                {
                                                                                    fieldLabel:'选择用户',
                                                                                    displayField:'email',
                                                                                    store:Ext.create('Ext.data.Store', {
                                                                                        fields:['id','email'],
                                                                                        proxy:{
                                                                                            type:'ajax',
                                                                                            url:'<%= admin_users_path %>',
                                                                                            reader:{
                                                                                                type:'json',
                                                                                                totalProperty:'totalCount',
                                                                                                root:'users'
                                                                                            }
                                                                                        }
                                                                                    }),
                                                                                    valueField:'id',
                                                                                    name:'user_id',
                                                                                    allowBlank:false,
                                                                                    blankText:'用户不能为空',
                                                                                    editable:false
                                                                                },
                                                                                {
                                                                                    fieldLabel:'选择角色',
                                                                                    displayField:'name',
                                                                                    valueField:'id',
                                                                                    store:Ext.create('Ext.data.Store', {
                                                                                        fields:['id','name'],
                                                                                        proxy:{
                                                                                            type:'ajax',
                                                                                            url:'<%= admin_roles_path %>',
                                                                                            reader:{
                                                                                                type:'json',
                                                                                                totalProperty:'totalCount',
                                                                                                root:'roles'
                                                                                            }
                                                                                        }
                                                                                    }),
                                                                                    name:'role_id',
                                                                                    allowBlank:false,
                                                                                    blankText:'角色不能为空',
                                                                                    editable:false
                                                                                }
                                                                            ],
                                                                            buttons:[
                                                                                {
                                                                                    text:'保存',
                                                                                    handler:function() {
                                                                                        var form = this.up('form').getForm();
                                                                                        if (form.isValid()) {
                                                                                            form.submit({
                                                                                                success: function(form, action) {
                                                                                                    appGridStore.load();
                                                                                                    editAppForUserWin.close();
                                                                                                    if (action.result.errors.app_id) {
                                                                                                        Ext.Msg.alert('提示', action.result.errors.app_id[0]);
                                                                                                        return;
                                                                                                    }
                                                                                                    Ext.data.StoreManager.lookup('editAppStore' + record.get('id')).load();
                                                                                                },
                                                                                                failure: function(form, action) {
                                                                                                    appGridStore.load();
                                                                                                    editAppForUserWin.close();
                                                                                                    if (action.result.errors.app_id) {
                                                                                                        Ext.Msg.alert('提示', action.result.errors.app_id[0]);
                                                                                                        return;
                                                                                                    }
                                                                                                    Ext.data.StoreManager.lookup('editAppStore' + record.get('id')).load();
                                                                                                }
                                                                                            });
                                                                                        }
                                                                                    }
                                                                                },
                                                                                {
                                                                                    text:'取消',
                                                                                    handler:function() {
                                                                                        this.up('window').close();
                                                                                    }
                                                                                }
                                                                            ]
                                                                        })
                                                                    ]
                                                                });
                                                                editAppForUserWin.show();
                                                            }
                                                        }
                                                    ],
                                                    listeners: {
                                                        edit:function(editor, e) {
                                                            editor.record.commit();
                                                            var record = editor.record;
                                                            Ext.Ajax.request({
                                                                url:"<%= admin_apps_path %>/" + record.get('app_id') + "/update_app_user_role",
                                                                method:'PUT',
                                                                params:{
                                                                    authenticity_token:$('meta[name="csrf-token"]').attr('content'),
                                                                    'stakeholder_id':record.get('id'),
                                                                    'user_id':record.get('user_id'),
                                                                    'role_id':record.get('role_id')
                                                                },
                                                                callback:function(options, success, response) {
                                                                }
                                                            });
                                                        }
                                                    }
                                                }
                                            ]
                                        });
                                        editAppWin.show();
                                    }
                                }
                            ]
                        }
                    ],
                    tbar: [
                        {
                            text: '增加应用',
                            iconCls:'add',
                            handler : function() {
                                var addAppWin = Ext.create('Ext.Window', {
                                    title:'增加应用',
                                    layout:'border',
                                    width:300,
                                    height:150,
                                    items:[
                                        Ext.create('Ext.form.Panel', {
                                            region:'center',
                                            frame:'true',
                                            url:"<%= admin_apps_path %>",
                                            defaultType:'textfield',
                                            defaults: {
                                                labelWidth:90,
                                                anchor:'95%'
                                            },
                                            items:[
                                                {
                                                    xtype:'hidden',
                                                    name:'authenticity_token',
                                                    value:$('meta[name="csrf-token"]').attr('content')
                                                },
                                                {
                                                    fieldLabel:'应用名',
                                                    name:'app[name]',
                                                    allowBlank:false,
                                                    blankText:'应用名不能为空'
                                                }
                                            ],
                                            buttons:[
                                                {
                                                    text:'保存',
                                                    handler:function() {
                                                        var form = this.up('form').getForm();
                                                        if (form.isValid()) {
                                                            form.submit({
                                                                success: function(form, action) {
                                                                    appGridStore.load();
                                                                    addAppWin.close();
                                                                },
                                                                failure: function(form, action) {
                                                                    appGridStore.load();
                                                                    addAppWin.close();
                                                                }
                                                            });
                                                        }
                                                    }
                                                },
                                                {
                                                    text:'取消',
                                                    handler:function() {
                                                        this.up('window').close();
                                                    }
                                                }
                                            ]
                                        })
                                    ]
                                });
                                addAppWin.show();
                            }
                        }
                    ],
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: appGridStore,
                        displayInfo: true
                    }),
                    listeners: {
                        edit:function(editor, e) {
                            editor.record.commit();
                            var record = editor.record;
                            Ext.Ajax.request({
                                url:"<%= admin_apps_path %>/" + record.get('id'),
                                method:'PUT',
                                params:{
                                    authenticity_token:$('meta[name="csrf-token"]').attr('content'),
                                    'app[name]':record.get('name')
                                },
                                callback:function(options, success, response) {
                                }
                            });
                        }
                    }
                }
            ]
        };
        //机房管理面板
        var roomPanel = {
            id:'5',
            title:'机房管理',
            frame:true,
            bodyPadding:5,
            layout:'border',
            split:true,
            fieldDefaults: {
                labelAlign: 'left',
                msgTarget: 'side'
            },
            items: [
                {
                    region:'center',
                    xtype: 'gridpanel',
                    title:'当前系统所有机房',
                    store:roomGridStore,
                    split:true,
                    columnLines:true,
                    viewConfig: {
                        stripeRows: true
                    },
                    selType: 'rowmodel',
                    plugins: [
                        roomPanelRowEditing
                    ],
                    columns:[
                        {
                            text:'机房名',
                            dataIndex:'name',
                            flex:1,
                            editor: {
                                xtype: 'textfield',
                                allowBlank: false
                            }
                        },
                        {
                            flex:1,
                            xtype: 'actioncolumn',
                            items: [
                                {
                                    icon   : "<%= root_path %>" + 'stylesheets/images/delete.gif',
                                    tooltip: '删除当前机房',
                                    handler: function(grid, rowIndex, colIndex) {
                                        var r = roomGridStore.getAt(rowIndex);
                                        var cascade = Ext.getCmp('cascadeMachine').checked;
                                        Ext.Ajax.request({
                                            url:"<%= admin_rooms_path %>/" + r.get('id'),
                                            method:'DELETE',
                                            params:{
                                                authenticity_token:$('meta[name="csrf-token"]').attr('content'),
                                                cascade:cascade
                                            },
                                            callback:function (options, success, response) {

                                            }
                                        });
                                        roomGridStore.remove(r);
                                        roomGridStore.load();
                                    }
                                }
                            ]
                        }
                    ],
                    tbar: [
                        {
                            text: '增加机房',
                            iconCls:'add',
                            handler : function() {
                                var addRoomWin = Ext.create('Ext.Window', {
                                    title:'增加机房',
                                    layout:'border',
                                    width:300,
                                    height:150,
                                    items:[
                                        Ext.create('Ext.form.Panel', {
                                            region:'center',
                                            frame:'true',
                                            url:"<%= admin_rooms_path %>",
                                            defaultType:'textfield',
                                            defaults: {
                                                labelWidth:90,
                                                anchor:'95%'
                                            },
                                            items:[
                                                {
                                                    xtype:'hidden',
                                                    name:'authenticity_token',
                                                    value:$('meta[name="csrf-token"]').attr('content')
                                                },
                                                {
                                                    fieldLabel:'机房名',
                                                    name:'room[name]',
                                                    allowBlank:false,
                                                    blankText:'机房名不能为空'
                                                }
                                            ],
                                            buttons:[
                                                {
                                                    text:'保存',
                                                    handler:function() {
                                                        var form = this.up('form').getForm();
                                                        if (form.isValid()) {
                                                            form.submit({
                                                                success: function(form, action) {
                                                                    roomGridStore.load();
                                                                    editMachineRoomComboStore.load();
                                                                    addRoomWin.close();
                                                                },
                                                                failure: function(form, action) {
                                                                    roomGridStore.load();
                                                                    editMachineRoomComboStore.load();
                                                                    addRoomWin.close();
                                                                }
                                                            });
                                                        }
                                                    }
                                                },
                                                {
                                                    text:'取消',
                                                    handler:function() {
                                                        this.up('window').close();
                                                    }
                                                }
                                            ]
                                        })
                                    ]
                                });
                                addRoomWin.show();
                            }
                        },
                        {
                            iconCls:'delete',
                            disabled:true
                        },
                        {
                            boxLabel:'删除机房时同时删除其下的所有机器',
                            xtype:'checkbox',
                            id:'cascadeMachine'
                        }
                    ],
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: roomGridStore,
                        displayInfo: true
                    }),
                    listeners: {
                        edit:function(editor, e) {
                            editor.record.commit();
                            var record = editor.record;
                            Ext.Ajax.request({
                                url:"<%= admin_rooms_path %>/" + record.get('id'),
                                method:'PUT',
                                params:{
                                    authenticity_token:$('meta[name="csrf-token"]').attr('content'),
                                    'room[name]':record.get('name')
                                },
                                callback:function(options, success, response) {
                                }
                            });
                        }
                    }
                }
            ]
        };
        //机器管理面板
        var adapterData = [
            {
                name:'ssh'
            }
        ];
        var machinePanel = {
            id: '6',
            title:'机器管理',
            frame:true,
            bodyPadding:5,
            layout:'border',
            split:true,
            fieldDefaults: {
                labelAlign: 'left',
                msgTarget: 'side'
            },
            items: [
                {
                    region:'center',
                    xtype: 'gridpanel',
                    title:'当前系统所有机器',
                    store:machineGridStore,
                    split:true,
                    columnLines:true,
                    viewConfig: {
                        stripeRows: true
                    },
                    selType: 'rowmodel',
                    plugins: [
                        machinePanelRowEditing
                    ],
                    columns:[
                        {
                            text:'机器名',
                            dataIndex:'name',
                            flex:6,
                            editor: {
                                xtype: 'textfield',
                                allowBlank: false
                            }
                        },
                        {
                            text:'主机名',
                            dataIndex:'host',
                            flex:6,
                            editor: {
                                xtype:'textfield'
                            }
                        },
                        {
                            text:'端口号',
                            dataIndex:'port',
                            flex:6,
                            editor: {
                                xtype:'numberfield'
                            }
                        },
                        {
                            text:'连接方式',
                            dataIndex:'adapter',
                            flex:6,
                            editor: {
                                xtype:'combo',
                                valueField:'name',
                                displayField:'name',
                                store:Ext.create('Ext.data.Store', {
                                    fields:['name'],
                                    data:adapterData
                                }),
                                editable:false
                            }
                        },
                        {
                            text:'机房',
                            dataIndex:'room_id',
                            flex:4,
                            editor: {
                                xtype:'combo',
                                valueField:'id',
                                displayField:'name',
                                store:editMachineRoomComboStore,
                                editable:false
                            },
                            renderer:roomRender
                        },
                        {
                            text:'应用',
                            dataIndex:'app_id',
                            flex:4,
                            editor: {
                                xtype:'combo',
                                valueField:'id',
                                displayField:'name',
                                store:editMachineAppComboStore,
                                editable:false
                            },
                            renderer:appRender
                        },
                        {
                            text:'用户名',
                            dataIndex:'user',
                            flex:4,
                            editor: {
                                xtype: 'textfield'
                            }
                        },
                        {
                            text:'密码',
                            dataIndex:'password',
                            flex:4,
                            editor: {
                                xtype: 'textfield',
                                inputType:'password'
                            },
                            renderer:passwordRender
                        },
                        {
                            flex:1,
                            xtype: 'actioncolumn',
                            items: [
                                {
                                    icon   : "<%= root_path %>" + 'stylesheets/images/delete.gif',
                                    tooltip: '删除当前机器',
                                    handler: function(grid, rowIndex, colIndex) {
                                        var r = machineGridStore.getAt(rowIndex);
                                        Ext.Ajax.request({
                                            url:"<%= admin_machines_path %>/" + r.get('id'),
                                            method:'DELETE',
                                            params:{
                                                authenticity_token:$('meta[name="csrf-token"]').attr('content')
                                            },
                                            callback:function (options, success, response) {

                                            }
                                        });
                                        machineGridStore.remove(r);
                                        machineGridStore.reload();
                                    }
                                }
                            ]
                        }
                    ],
                    tbar: [
                        {
                            text: '增加机器',
                            iconCls:'add',
                            handler : function() {
                                var addMachineWin = Ext.create('Ext.Window', {
                                    title:'增加机器',
                                    layout:'border',
                                    width:500,
                                    items:[
                                        Ext.create('Ext.form.Panel', {
                                            region:'center',
                                            frame:'true',
                                            url:"<%= admin_machines_path %>",
                                            defaultType:'textfield',
                                            defaults: {
                                                labelWidth:90,
                                                anchor:'95%'
                                            },
                                            items:[
                                                {
                                                    xtype:'hidden',
                                                    name:'authenticity_token',
                                                    value:$('meta[name="csrf-token"]').attr('content')
                                                },
                                                {
                                                    fieldLabel:'机器名',
                                                    name:'machine[name]',
                                                    allowBlank:false,
                                                    blankText:'机器名不能为空'
                                                },
                                                {
                                                    fieldLabel:'主机名',
                                                    name:'machine[host]'
                                                },
                                                {
                                                    fieldLabel:'端口号',
                                                    xtype:'numberfield',
                                                    name:'machine[port]'
                                                },
                                                {
                                                    fieldLabel:'连接方式',
                                                    xtype:'combo',
                                                    name:'machine[adapter]',
                                                    valueField:'name',
                                                    displayField:'name',
                                                    store:Ext.create('Ext.data.Store', {
                                                        fields:['name'],
                                                        data:adapterData
                                                    }),
                                                    editable:false
                                                },
                                                {
                                                    fieldLabel:'机房',
                                                    xtype:'combo',
                                                    name:'machine[room_id]',
                                                    valueField:'id',
                                                    displayField:'name',
                                                    store:addMachineRoomComboStore,
                                                    editable:false
                                                },
                                                {
                                                    fieldLabel:'应用',
                                                    xtype:'combo',
                                                    name:'machine[app_id]',
                                                    valueField:'id',
                                                    displayField:'name',
                                                    store:addMachineAppComboStore,
                                                    editable:false
                                                }
                                            ],
                                            buttons:[
                                                {
                                                    text:'保存',
                                                    handler:function() {
                                                        var form = this.up('form').getForm();
                                                        if (form.isValid()) {
                                                            form.submit({
                                                                success: function(form, action) {
                                                                    machineGridStore.load();
                                                                    addMachineRoomComboStore.load();
                                                                    addMachineAppComboStore.load();
                                                                    addMachineWin.close();
                                                                },
                                                                failure: function(form, action) {
                                                                    if (action.result.errors.port) {
                                                                        Ext.Msg.alert('提醒', action.result.errors.port[0]);
                                                                        return;
                                                                    }
                                                                    machineGridStore.load();
                                                                    addMachineRoomComboStore.load();
                                                                    addMachineAppComboStore.load();
                                                                    addMachineWin.close();
                                                                }
                                                            });
                                                        }
                                                    }
                                                },
                                                {
                                                    text:'取消',
                                                    handler:function() {
                                                        this.up('window').close();
                                                    }
                                                }
                                            ]
                                        })
                                    ]
                                });
                                addMachineWin.show();
                            }
                        }
                    ],
                    bbar: Ext.create('Ext.PagingToolbar', {
                        store: machineGridStore,
                        displayInfo: true
                    }),
                    listeners: {
                        edit:function(editor, e) {
                            var record = editor.record;
                            var params = {
                                authenticity_token:$('meta[name="csrf-token"]').attr('content'),
                                'machine[name]':record.get('name'),
                                'machine[host]':record.get('host'),
                                'machine[room_id]':record.get('room_id'),
                                'machine[app_id]':record.get('app_id'),
                                'machine[user]':record.get('user'),
                                'machine[port]':record.get('port'),
                                'machine[adapter]':record.get('adapter')
                            };
                            if (!!record.get('password')) {
                                params['machine[password]'] = record.get('password');
                                record.set('password', '');
                            }
                            Ext.Ajax.request({
                                url:"<%= admin_machines_path %>/" + record.get('id'),
                                method:'PUT',
                                params:params,
                                callback:function(options, success, response) {
                                    var result = Ext.decode(response.responseText);
                                    if (result.errors.port) {
                                        Ext.Msg.alert('提醒', result.errors.port[0]);
                                        record.reject();
                                        return;
                                    }
                                    record.commit();
                                }
                            });
                        }
                    }
                }
            ]
        };

        //管理操作区
        var adminDirectivePanel = Ext.create('Ext.panel.Panel', {
            region: 'center',
            layout: 'card',
            activeItem: 0,
            border:false,
            items:[
                userInfoPanel,
                rolePanel,
                cmdDefPanel,
                cmdGroupDefPanel,
                appPanel,
                roomPanel,
                machinePanel
            ]
        });

        adminMenuTreePanel.getSelectionModel().on('select', function(selModel, record) {
            adminDirectivePanel.layout.setActiveItem(record.get('id'));
            if (record.get('id') == 2) {
                editDirectiveTemplateDirectiveGroupComboStore.load();
                cmdDefGridStore.load();
            }
            if (record.get('id') == 6) {
                editMachineRoomComboStore.load();
                editMachineAppComboStore.load();
                machineGridStore.load();
            }
        });

        Ext.create('Ext.Viewport', {
            layout: {
                type: 'border',
                padding: 5
            },
            defaults: {
                split:true
            },
            items: [
                welcomePanel,
                adminMenuTreePanel,
                adminDirectivePanel
            ]
        });
    });
</script>